home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / accounts / acct-1.3 / acct-1 / acct-1.3.73 / scripts / handleacct.sh
Linux/UNIX/POSIX Shell Script  |  1996-03-12  |  3KB  |  140 lines

  1. #!/bin/sh
  2. #
  3. # handleacct.sh
  4. # Handles /var/log/acct file and old acct files
  5. # Run this script periodically from root's crontab
  6. # Sample line to add into /etc/crontab:
  7. #00 * * * * root /usr/local/sbin/handleacct.sh -c -s 97
  8. #
  9. # For explanation of options see builtin help in this script
  10. #
  11. # Use this script at your own risk. No guarantee or warranty of
  12. # any kind of is given against this script to destroy all files
  13. # in your system or cause other damage. Read this script
  14. # carefully before using it. Modify it to suite to your local
  15. # environment.
  16. #
  17. # Apply GPL as copyright to this file. 
  18. #
  19. # Author: <jiivee@iki.fi> Juha Virtanen
  20. # Last time modified on 12th March 1996
  21.  
  22. PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin; export PATH
  23.  
  24. prog=`basename $0`
  25. version=1.3.73
  26.  
  27. usage="Usage: ${prog}: [-v] [-h] [-c] [-s #] [-f file] [-d dir]"
  28.  
  29. # Log to syslog what we do.
  30. # To not log, comment logging line out
  31. slog ()
  32. {
  33.     logger -t "$prog" -i -p user.notice $*
  34. }
  35.  
  36. # Where are old files saved
  37. storedir=/var/log/accounting
  38. storefile="${storedir}/`date +acct-%Y%m%d-%H%M`"
  39. acctfile="/var/log/acct"
  40. # Remove old accounting files after N hours, default is 25 hours.
  41. rmtime=25
  42. # By default, do not compress old files
  43. compress=0
  44.  
  45. while [ $# -gt 0 ]; do
  46.     case $1 in
  47.         -c|--compress)
  48.             compress=1
  49.         ;;
  50.         -d|--storedir)
  51.             storedir=$2
  52.             shift
  53.         ;;
  54.         -f|--acct_file)
  55.             acctfile=$2
  56.             shift
  57.         ;;
  58.         -s|--save-time)
  59.             rmtime=$2
  60.             shift
  61.         ;;
  62.         -v|--version)
  63.             echo "${prog} version is $version"
  64.             exit 0
  65.         ;;
  66.         -h|--help)
  67.             echo "$usage"
  68.             echo ""
  69.             echo "-h|--help           This help"
  70.             echo "-c|--compress       Compress old files with gzip"
  71.             echo "-d|--storedir dir   Directory to put old files to (${storedir})"
  72.             echo "-f|--acct-file file Current accounting file (${acctfile})"
  73.             echo "-s|--save-time #    How many hours to keep old accounting files (${rmtime} hours)"
  74.             echo "-v|--version        Print version info"
  75.             exit 0
  76.         ;;
  77.         *)
  78.             echo "Unknown option -- $1" 1>&2
  79.             echo "$usage" 1>&2
  80.             exit 2
  81.         ;;
  82.     esac
  83.     shift
  84. done
  85.  
  86. if [ ! -f $acctfile ]; then
  87.     slog "${acctfile}: Not found or not a regular file."
  88.     echo "${prog}: ${acctfile}: Not found or not a regular file." 1>&2
  89.     exit 1
  90. fi
  91. if [ ! -d $storedir ]; then
  92.     mkdir -p $storedir
  93.     if [ $? != 0 ]; then
  94.         slog "mkdir: cannot make directory $storedir"
  95.         echo "${prog}: mkdir: cannot make directory $storedir" 1>&2
  96.         exit 2
  97.     fi
  98.     chown root.adm $storedir
  99.     # Prevent curious people seeing old accounting data
  100.     chmod 2750 $storedir
  101. fi
  102.  
  103. # Move old file to new name and create new file
  104. # before turning off and on accounting to minimalize the time
  105. # we are running without accounting
  106. slog "Handling $acctfile"
  107. rm -f ${acctfile}.tmp
  108. mv $acctfile ${acctfile}.tmp
  109. touch $acctfile
  110. accton
  111. accton $acctfile
  112. mv -f ${acctfile}.tmp $storefile
  113. # Allow everybody to examine current accounting file
  114. chown root.adm $acctfile
  115. chmod 0755 $acctfile
  116.  
  117. slog "Handling $storefile"
  118. tmpfile=/tmp/${prog}.$$
  119. acctentries $storefile 2>&1 > $tmpfile
  120. cat $tmpfile | slog
  121. rm -f $tmpfile
  122. # Compress old files to save space
  123. if [ $compress = 1 ]; then
  124.     rm -f ${storefile}.gz
  125.     nice -n 14 gzip -9 $storefile
  126. fi
  127. sync
  128.  
  129. # Remove old accounting files after N hours
  130. slog "Old accounting files removed after $rmtime hours."
  131. time=`expr $rmtime \* 60`
  132. storedir=`dirname $storefile`
  133. for f in `find $storedir -maxdepth 1 -type f -mmin +$time -print`; do
  134.     slog "removing $f"
  135.     rm -f $f
  136. done
  137.  
  138. #slog "Done."
  139. #EOF
  140.